#!/bin/sh
# PCP QA Test No. 1547
# Exercise pmrep handling PMCD metric-names-change notification.
#
# Copyright (c) 2018 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

. ./common.python

$python -c "from pcp import pmapi" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python pcp pmapi module not installed"
$python -c "from collections import OrderedDict" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python collections OrderedDict module not installed"

which pmrep >/dev/null 2>&1 || _notrun "No pmrep binary installed"

debug=`pmprobe -v pmcd.control.debug | $PCP_AWK_PROG '{print $3}'`
[ "$debug" != '""' ] && echo "Warning: pmcd debug $debug, not \"\"!"

_cleanup()
{
    cd $here
    $sudo rm -f $PCP_TMP_DIR/mmv/test
    _restore_pmda_mmv
    $sudo rm -rf $tmp $tmp.*
}

_filter_value()
{
    sed \
        -e 's,[0-9]\{4\,7\},xxxxxxx,g' \
    #end
}

status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

if [ $PCP_PLATFORM = freebsd ]
then
    # workaround datetime.strftime() in Python that is busted when
    # the timezone|timeinfo is, er, Australia/Someplace, for at least
    # some parts of the year
    #
    version=`uname -rs | sed -e 's/FreeBSD //'`
    case "$version"
    in
	14.[0-3]-*)
	    export TZ=UTC
	    echo "Forcing TZ=UTC to dodge FreeBSD bug" >>$seq_full
	    ;;
    esac
fi

# real QA test starts here
_prepare_pmda_mmv

echo Running tests with pmrep ...

# test names-change=abort
echo testing names-change=abort
rm -f $PCP_TMP_DIR/mmv/test
pmstore mmv.control.reload 1 >>$seq_full
pmrep -s 4 -t 1 --names-change=abort mmv.control.files | _filter_value &

pmsleep 1.5
$here/src/mmv_genstats test 1
pmstore mmv.control.reload 1 >>$seq_full
wait

# test names-change=ignore
echo testing names-change=ignore
rm -f $PCP_TMP_DIR/mmv/test
pmstore mmv.control.reload 1 >>$seq_full
pmrep -s 4 -t 1 -r --names-change=ignore mmv | _filter_value &

pmsleep 1.5
$here/src/mmv_genstats test 1
pmstore mmv.control.reload 1 >>$seq_full
wait

# test names-change=update
echo testing names-change=update
rm -f $PCP_TMP_DIR/mmv/test
pmstore mmv.control.reload 1 >>$seq_full
pmrep -s 5 -t 1 -r --names-change=update mmv | _filter_value &

pmsleep 1.5
$here/src/mmv_genstats test 2
pmstore mmv.control.reload 1 >>$seq_full
wait

# test names-change=update
echo testing names-change=update with instance filtering
rm -f $PCP_TMP_DIR/mmv/test
pmstore mmv.control.reload 1 >>$seq_full
pmrep -s 10 -t 1 -i zero -i eggs -r --names-change=update mmv | _filter_value &

pmsleep 1.5
$here/src/mmv_genstats test 3
rm -f $PCP_TMP_DIR/mmv/test
pmstore mmv.control.reload 1 >>$seq_full
pmsleep 2
$here/src/mmv_genstats test 3
pmstore mmv.control.reload 1 >>$seq_full
wait

# success, all done
status=0
exit
